/**
 * Created by zjx on 2017/1/16.
 */
! function uploadMultiImages($){

    var csrf_token = $('#csrfToken').text();
    
    var $form = $('#fileupload');
    var $form_2 = $('#fileupload_2');
    var $form_3 = $('#fileupload_3');

    var $product_selector = $('#product_selections');
    var $product_selector_2 = $('#product_selections_2');
    var $product_selector_3 = $('#product_selections_3');

    var $sort_btn = $('#sort_btn');
    var $sort_btn_2 = $('#sort_btn_2');
    var $sort_btn_3 = $('#sort_btn_3');

    var upload_img_count = 0;
    var start_uploading = false;

    $form.fileupload({
        url: $form.attr('action')
    });

    $form
        .bind('fileuploadsubmit', function (e, data) {
            if ( !  $product_selector.val() ) {
                alert('请先选择产品');
                return false;
            }

            data.formData = {
                url : $form.attr('action'),
                _token: csrf_token,
                product_id: $product_selector.val()
            };
        })
        .bind('fileuploadsend', function ( e, data ) {
            App.blockUI($('.portlet'));
        })
        .bind('fileuploaddone', function ( e, data ) {
            App.unblockUI($('.portlet'));
            upload_img_count--;
            if ( ! upload_img_count ) {
                setTimeout(function(){
                    $sort_btn.trigger('click');
                    start_uploading = false;
                },1000);
            }
        });


    $('#upload_all_img_btn').on('click',function(){
        upload_img_count = 0;
        $form.find('tr').each( function( index , tr ) {
            if ( ! tr.dataset.key ) {
                upload_img_count++;
            }
        });
    });

    $product_selector.on('change' , function(){

        var product_id = $(this).val();
        $form.find('tbody').empty();
        $sort_btn.hide();

        if ( ! product_id ) return false;

        App.blockUI($('.portlet'));
        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: $form.fileupload('option', 'url') + '/' + product_id,
            dataType: 'json',
            context: $form[0]
        })
            .done(function (result) {
                if ( result.length ) $sort_btn.show();

                $(this).fileupload('option', 'done')
                    .call(this, null, {
                        result: { files : result }
                    });
            })
            .always(function(){
                App.unblockUI($('.portlet'));
            });

    });

    $form_2.fileupload({
        url: $form_2.attr('action')
    });

    $form_2
        .bind('fileuploadsubmit', function (e, data) {
            if ( !  $product_selector_2.val() ) {
                alert('请先选择产品');
                return false;
            }

            data.formData = {
                url : $form_2.attr('action'),
                _token: csrf_token,
                product_id: $product_selector_2.val()
            };
        })
        .bind('fileuploadsend', function ( e, data ) {
            App.blockUI($('.portlet'));
        })
        .bind('fileuploaddone', function ( e, data ) {
            App.unblockUI($('.portlet'));
            upload_img_count--;
            if ( ! upload_img_count ) {
                setTimeout(function(){
                    $sort_btn_2.trigger('click');
                    start_uploading = false;
                },1000);
            }
        });


    $('#upload_all_img_btn_2').on('click',function(){
        upload_img_count = 0;
        $form_2.find('tr').each( function( index , tr ) {
            if ( ! tr.dataset.key ) {
                upload_img_count++;
            }
        });
    });

    $product_selector_2.on('change' , function(){

        var product_id = $(this).val();
        $form_2.find('tbody').empty();
        $sort_btn_2.hide();

        if ( ! product_id ) return false;
        App.blockUI($('.portlet'));

        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: $form_2.fileupload('option', 'url') + '/' + product_id,
            dataType: 'json',
            context: $form_2[0]
        })
            .done(function (result) {
                if ( result.length ) $sort_btn_2.show();

                $(this).fileupload('option', 'done')
                    .call(this, null, {
                        result: { files : result }
                    });
            })
            .always(function(){
                App.unblockUI($('.portlet'));
            });

    });


    $form_3.fileupload({
        url: $form_3.attr('action')
    });

    $form_3
        .bind('fileuploadsubmit', function (e, data) {
            if ( !  $product_selector_3.val() ) {
                alert('请先选择产品');
                return false;
            }

            data.formData = {
                url : $form_3.attr('action'),
                _token: csrf_token,
                product_id: $product_selector_3.val()
            };
        })
        .bind('fileuploadsend', function ( e, data ) {
            App.blockUI($('.portlet'));
        })
        .bind('fileuploaddone', function ( e, data ) {
            App.unblockUI($('.portlet'));
            upload_img_count--;
            if ( ! upload_img_count ) {
                setTimeout(function(){
                    $sort_btn_3.trigger('click');
                    start_uploading = false;
                },1000);
            }
        });


    $('#upload_all_img_btn_3').on('click',function(){
        upload_img_count = 0;
        $form_3.find('tr').each( function( index , tr ) {
            if ( ! tr.dataset.key ) {
                upload_img_count++;
            }
        });
    });

    $product_selector_3.on('change' , function(){

        var product_id = $(this).val();
        $form_3.find('tbody').empty();
        $sort_btn_3.hide();

        if ( ! product_id ) return false;
        App.blockUI($('.portlet'));

        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: $form_3.fileupload('option', 'url') + '/' + product_id,
            dataType: 'json',
            context: $form_3[0]
        })
            .done(function (result) {
                if ( result.length ) $sort_btn_2.show();

                $(this).fileupload('option', 'done')
                    .call(this, null, {
                        result: { files : result }
                    });
            })
            .always(function(){
                App.unblockUI($('.portlet'));
            });

    });

}(window.jQuery);